package com.woniuxy.sale.controller;

import com.woniuxy.response.CustomPage;
import com.woniuxy.response.ResponseResult;
import com.woniuxy.model.entity.ExportTable;
import com.woniuxy.sale.entity.dto.SalePackManageDTO;
import com.woniuxy.sale.entity.vo.*;
import com.woniuxy.sale.service.SalePackService;

import com.woniuxy.sale.util.MyExcelUtils;
import com.woniuxy.util.CheckTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 打包单; 前端控制器
 * </p>
 *
 * @author lio
 * @since 2023-09-07
 */
@Slf4j
@RestController
@RequestMapping("/salePack")
public class SalePackController {

    @Resource
    private SalePackService salePackService;

    /**
     * 新增打包单 sno
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody SalePackVo salePackVo, HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");
        log.debug(operator);
        salePackVo.setOperator(operator);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",salePackService.add(salePackVo));
    }

    /**
     * 提交打包单,弃用
     */
    @PostMapping("/updateState")
    public ResponseResult<Boolean> updateState(@RequestBody UpdateSalePackVo updateSalePackVo,HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String operator = (String) user.get("account");
        log.debug(operator);
        updateSalePackVo.setOperator(operator);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",salePackService.updateState(updateSalePackVo));
    }

    /**
     * 管理打包单,即显示打包单详情
     */
    @PostMapping("/managePack")
    public ResponseResult<CustomPage<List<SalePackManageDTO>>>managePack(@RequestBody SalePackManageVo salePackManageVo){

        CustomPage<List<SalePackManageDTO>> list = salePackService.managePack(salePackManageVo);
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",list);
    }

    /**
     * 查询所有pno
     */
    @GetMapping("findAllPno")
    public ResponseResult<List<String>> findAllPno(){

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",salePackService.findAllPno());
    }

    /**
     * 导出成excel
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("exportSalePack")
    public void exportSalePack(@RequestBody SalePackManageVo salePackManageVo, HttpServletResponse response,HttpServletRequest request) {

        List<SalePackManageDTO> list = salePackService.managePack(salePackManageVo).getData();
        log.debug(list.toString());

        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String account = (String) user.get("account");
        ExportTable table = new ExportTable();
        table.setAccount(account);
        table.setTableName("打包单");
        boolean flag = salePackService.exportSalePack(table);

        response.addHeader("Access-Control-Expose-Headers","exportFlag");
        response.addHeader("exportFlag",String.valueOf(flag));

        MyExcelUtils.exportExcel(list,
                "打包单",
                "打包单列表",
                SalePackManageDTO.class,
                "打包单"+time+".xls",response);
    }

    /**
     * 填写需求单
     */
    @PreAuthorize("hasAnyAuthority('sale:agreePack','sale:addPack')")
    @PostMapping("/addDemand")
    public ResponseResult<Boolean> addDemand(@RequestBody AddDemandVo addDemandVo, HttpServletRequest request){

        String aid = request.getHeader("access_token");
        Map<String,Object> user = CheckTokenUtil.getUser(aid);
        assert user != null;
        String createUser = (String) user.get("account");
        addDemandVo.setCreateUser(createUser);

        return new ResponseResult<>(ResponseResult.SUCCESS,"success",salePackService.addDemand(addDemandVo));
    }
}

